<?php defined('SYSPATH') or die('No direct script access.');

class Helper_Auth
{
	// user data
	private $user_id 	= NULL;
	private $user 		= NULL;
	private $remember 	= TRUE;
	private $logged_in 	= FALSE;
	
	private function __construct($user_id, $user_data)
	{
		if ($user_id != NULL && $user_data != NULL)
		{
			$this->user_id = $user_id;
			$this->user = $user_data;
			$this->logged_in = TRUE;
		}
	}
	
	public static function instance()
	{
		// Get user from Session
		$user_id 	= Session::instance()->get('user_id');
		$user_data 	= Session::instance()->get('user');
		$user_auth 	= new Helper_Auth($user_id, $user_data);
		
		return $user_auth;
	}
	
	public function logged_in($role = array())
	{
		if ($this->logged_in == FALSE)
			return FALSE;
			
//		if (count($role) == 0 || (count($role) > 0 && $role[0] == 'user'))
//			return $this->logged_in;
//			
//		// Get current user role
//		$groups = Model::factory('user')->get_user_groups($this->user_id);
//		foreach ($groups as $group)
//			if (in_array($group['name'], $role))
//				return TRUE;
		
		$user = Model::factory('user')->get_user_info($this->user_id);
		$user_data = $this->user;
		if($user['id'] == $user_data['id'] && $user['password'] && $user['password'] == $user_data['password']) {
			return TRUE;
		}
				
		return FALSE;
	}
	
	public function login($username, $password, $remember = TRUE)
	{
		if (Model::factory('user')->exists_user($username, $password))
		{
			// Get user data
			$user = Model::factory('user')->get_user_info_by_email($username);
			
			// Store user data in session
			Session::instance()->set('user', $user);
    		Session::instance()->set('user_id', $user['id']);
    		
    		// Add code to remember user here
    		
    		return TRUE;
		}
		
		return FALSE;
	}
	
	public function facebook_login($facebook_id, $access_token) 
	{
		
		$user = Model::factory('user')->get_user_auth_by_id(Helper_MyConst::AUTH_FACEBOOK, $facebook_id);
		
		// update user with the new Facebook token
		if($user) {
			Model::factory('user')->update_user_auth_info($user['user_id'], Helper_MyConst::AUTH_FACEBOOK, array(
				'access_token' => $access_token
			));
		} else { // register new account
			return FALSE;
		}
		
		Session::instance()->set('user', $user);
    	Session::instance()->set('user_id', $user['id']);
		
		return $user;
	}
	
	
	public function logout()
	{
		Session::instance()->set('user', NULL);
    	Session::instance()->set('user_id', NULL);
	}
	
	static public function checklogin()
	{
		$user_id = Session::instance()->get('user_id');
		if ($user_id != NULL)
		{
			$user = Model::factory('user')->get_user_info($user_id);
			Session::instance()->set('user', $user);
		}
	}
	
	// Return user data from session
	public function get_user($fieldname = NULL)
	{
		if ($fieldname == NULL)
			return $this->user;
		else
			return $this->user[$fieldname];
	}
	
	// Return user id from session
	public function get_user_id()
	{
		return $this->user_id;
	}
	
	// Return user display name
	public function get_user_displayname()
	{
		return $this->get_user('displayname');
	}
}
